package com.yxuan.springboot.controller;


import com.alibaba.fastjson.JSON;
import com.yxuan.springboot.constant.SystemConstant;
import com.yxuan.springboot.model.AdminLogin;
import com.yxuan.springboot.service.AdminLoginService;
import com.yxuan.springboot.util.Code;
import com.yxuan.springboot.util.JwtUtil;
import com.yxuan.springboot.util.Result;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.Map;


import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 
 * @author zchao
 *   AdminLogin控制器
 * @date 2023-05-09 11:17:05
 */
//@CrossOrigin//开放前端的跨域访问，只需在每个controller上写这个注解，无需专门再写config配置类
@Slf4j
@Controller
@Api(tags = "管理员登录表")
@RequestMapping(value = "/adminlogin", method = {RequestMethod.POST,RequestMethod.GET})
public class AdminLoginController extends BaseController<AdminLogin>{


    @Autowired
    private AdminLoginService adminLoginService;

    //管理员账号密码登录
    @ResponseBody
    @RequestMapping(value = "/pass", method = RequestMethod.POST)
    public Result sendCode(@RequestBody AdminLogin adminLogin) throws Exception {


            //根据账号查询得到改管理员
            AdminLogin targetAdmin = adminLoginService.findObjectByPramas(adminLogin);
            //如果targetAdmin不为空表示登录成功，返回token，为空表示账号密码不正确
            Map<String, Object> map = new HashMap<>();
            if(targetAdmin!=null){


                //新建JWT加密对象
                log.info("新建JWT加密对象");
                AdminLogin JWTEmployee = new AdminLogin();
                JWTEmployee.setUserName(targetAdmin.getUserName());
                //把token返回给客户端-->客户端保存至localStorage-->客户端每次请求附带localStorage参数
                //JSON.toJSONString(admin):将JSON对象转为字符串
                //SystemConstant.JWT_TTL：token有效时间
                String JWT = JwtUtil.createJWT("1", JSON.toJSONString(JWTEmployee), SystemConstant.JWT_TTL);
                map.put("Token",JWT);
                log.info("token:" + JWT);
                //新建管理员对象，用于存放修改之后的JWT,
                AdminLogin returnLogin = new AdminLogin();
                returnLogin.setId(targetAdmin.getId());
                returnLogin.setToken(JWT);
                adminLoginService.upd(returnLogin);
                return new Result(map,SUCCESSCODE,"管理员登录成功");


            }
            map.put("token",null);
            map.put("原因","账号或密码错误");
            return new Result(map, Code.SELECT_SINGLE_BY_CONDITION_NO,"管理员登录失败");


    }




//    /**
//     * 管理员登录
//     */
//    @ApiOperation(value = "管理员登录", notes = "管理员登录")
//    @RequestMapping("/login")
//    @ResponseBody
//    public ResponseResult adminLogin(AdminLogin adminLogin){
//        Map<String,Object> map = new HashMap<>();
//        map.put("token","123123");
//        return ResponseResult.success(map);
//    }
//
//
//    /**
//     * 管理员退出登录
//     */
//    @ApiOperation(value = "管理员登录", notes = "管理员登录",httpMethod = "POST")
//    @RequestMapping("/logout")
//    @ResponseBody
//    public ResponseResult adminQuit(AdminLogin adminLogin){
//        Map<String,Object> map = new HashMap<>();
//        map.put("token","");
//        return ResponseResult.success(map);
//    }
}
